import { reqAddOrUpdateUser, reqBatchRemoveUser, reqRemoveUser, reqRoleList, reqUpdateRoles, reqUserList } from "@/api/acl/user"

const actions = {
    // 发送获取用户数据的action
    async getUserList({ commit }, data) {
        const { page, limit, username } = data
        const result = await reqUserList(page, limit, username)
        if (result.code === 20000) {
            commit('GETUSERLIST', result.data)
        }
    },
    // 发送添加用户|修改用户的action
    async addOrUpdateUser(_, user) {
        const result = await reqAddOrUpdateUser(user)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    },
    // 发送批量删除用户的action
    async batchRemoveUser(_, data) {
        const result = await reqBatchRemoveUser(data)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    },
    // 发送删除用户的action
    async reomveUser(_, id) {
        const result = await reqRemoveUser(id)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    },
    // 发送根据用户获取角色数据的action
    async getRoleList({commit}, userId) {
        const result = await reqRoleList(userId)
        if (result.code === 20000) {
            commit('GETROLELIST', result.data)
        }
    },
    // 发送分配角色的action
    async updateRoles(_, data) {
        const result = await reqUpdateRoles(data)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    }
}
const mutations = {
    GETUSERLIST(state, userList) {
        state.userList = userList
    },
    GETROLELIST(state, roleList) {
        state.roleList = roleList
    },
    CLEAR(state) {
        state.roleList = {}
    }
}
const state = {
    // 用户数据
    userList: {},
    // 角色数据 
    roleList: {}
}

export default {
    namespaced: true,
    actions,
    mutations,
    state
}